"""
@file:   user
@author: FrankYi
@date:   2020/08/14
@desc:
"""
from wtforms import Form, StringField
from wtforms.validators import DataRequired, Email, Regexp, ValidationError
from models.user import UserSouth



class UserForm(Form):
    email = StringField(validators=[DataRequired(), Email(message="邮箱不合法")])
    # 必填，大写，小写，数字，下划线组成的6~18位的字符串
    password = StringField(validators=[DataRequired(), Regexp(r'^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$', message="密码复杂度不符合要求")])


    # 自定义字段检查方法
    def validate_email(self, value):
        # 验证email是否在数据库已经存在
        if UserSouth.query.filter_by(useremail=value.data).first():
            raise ValidationError("邮箱已存在！")

    # # 用户名长度6~16位之间
    # def validate_username(self, value):
    #     value.data = "sanle-"+ value.data

    # # 整体验证
    def validate(self):
        return super().validate()

